<template>
  <div id="app">
    <el-form :inline="true" :model="formSelect" class="demo-form-inline">
      <el-form-item label="角色名称" prop="name">
        <el-input
          size="small"
          v-model="formSelect.name"
          clearable
          placeholder
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button
          size="small"
          type="primary"
          @click="getDataList()"
          icon="el-icon-search"
          >查询</el-button
        >
        <el-button size="small" type="success" @click="addOrUpdateHandle()"
          >新增</el-button
        >
        <el-button size="small" type="danger" @click="deleteHandle()"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      ></el-table-column>
      <el-table-column
        type="index"
        header-align="center"
        align="center"
        label="序号"
        width="50"
      ></el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="角色名称"
        width="350"
      ></el-table-column>
      <!-- <el-table-column prop="updateTime" header-align="center" :formatter="dateFormat" align="center" width="180" label="修改时间">
      </el-table-column>-->
      <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        label="角色说明"
      ></el-table-column>
      <!-- <el-table-column prop="authData" header-align="center" align="center" label="角色权限">
      </el-table-column>-->
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="success"
            size="small"
            @click="addOrUpdateHandle(scope.row, scope.$index)"
            >修改</el-button
          >
          <el-button
            type="danger"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
          <el-button
            type="warning"
            size="small"
            @click="RoleAuthorityHandle(scope.row)"
            >功能授权</el-button
          >
          <el-button
            type="primary"
            size="small"
            @click="RoleDistributionHandle(scope.row)"
            >分配用户</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      background
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      @getDataList="getDataList"
      ref="addOrUpdate"
    ></add-or-update>
    <role-Authority
      v-if="RoleAuthorityVisible"
      @getDataList="getDataList"
      ref="roleAuthority"
    ></role-Authority>
    <role-Distribution
      v-if="RoleDistributionVisible"
      @getDataList="getDataList"
      ref="roleDistribution"
    ></role-Distribution>
  </div>
</template>

<script>
import AddOrUpdate from './roleAddUpdata'
import RoleAuthority from './roleAuthority'
import RoleDistribution from './roleDistribution'
import { getListData, deleteRole } from '../../api/src/system/role'

export default {
  name: 'roleManagement',
  components: {
    AddOrUpdate,
    RoleAuthority,
    RoleDistribution,
  },
  data() {
    return {
      formSelect: {
        name: '',
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: this.totalPage,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      RoleAuthorityVisible: false,
      RoleDistributionVisible: false,
    }
  },
  created() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    async getDataList() {
      this.dataListLoading = true
      this.dataList = []
      let parameter

      if (this.formSelect.name.length == 0) {
        parameter =
          'pageNum=' + this.pageIndex + '&' + 'pageSize=' + this.pageSize
      } else {
        parameter =
          'name=' +
          this.formSelect.name +
          '&pageNum=' +
          this.pageIndex +
          '&' +
          'pageSize=' +
          this.pageSize
      }
      await getListData(parameter).then((res) => {
        if (res.records === 0) {
          this.dataList = []
          this.totalPage = 0
        } else {
          this.dataList = res.records
          this.totalPage = res.total
        }

        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    async addOrUpdateHandle(row, index) {
      this.addOrUpdateVisible = true
      await this.$nextTick(() => {
        this.$refs.addOrUpdate.init(row, index)
      })
    },
    // 权限设置
    RoleAuthorityHandle(row) {
      this.RoleAuthorityVisible = true
      this.$nextTick(() => {
        this.$refs.roleAuthority.init(row)
      })
    },
    //用户分配
    RoleDistributionHandle(row) {
      this.RoleDistributionVisible = true
      this.$nextTick(() => {
        this.$refs.roleDistribution.init(row)
      })
    },
    // 删除
    async deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id
          })

      ids.push(id)
      await this.$confirm(
        `确定进行[${id ? '删除' : '批量删除'}]操作?`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      )
        .then(() => {
          deleteRole(ids).then((res) => {
            this.$message.success('删除成功')
          })
        })
        .catch(() => {})
      this.dataListSelections = []
      setTimeout(this.getDataList, 800)
    },

    handleViewOrder(index, row) {
      this.$router.push({
        path: '/index',
        query: {
          id: row.id,
        },
      })
    },

    //时间格式化
    dateFormat: function (data) {
      let time = data.updateTime
      var date = new Date(time) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var Y = date.getFullYear() + '-'
      var M =
        (date.getMonth() + 1 < 10
          ? '0' + (date.getMonth() + 1)
          : date.getMonth() + 1) + '-'
      var D = date.getDate() + ' '
      var h = date.getHours() + ':'
      var m = date.getMinutes() + ':'
      var s = date.getSeconds()
      return Y + M + D + h + m + s
    },
  },
}
</script>

<style>
.el-pagination {
  display: flex;
  white-space: nowrap;
  padding: 20px 5px;
  color: #303133;
}
</style>
